﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class NHATHUOC_Control : IDisposable
    {

        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var list = db.NHATHUOC_Get_List();

            var typeorder = list.OrderBy(w => w.TEN_NHATHUOC);

            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_NHATHUOC";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            NHATHUOC_Get_ListResult temp = new NHATHUOC_Get_ListResult();
            temp.MA_NHATHUOC = "...";
            
            temp.TEN_NHATHUOC = "Thêm mới";
            cmb.Items.Add(temp);
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.NHATHUOC_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "NT0001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "NT";
                    for (int j = 0; j < (4 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.NHATHUOC_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_NHATHUOC.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(string ma_nt, TextBox txtTen_NhaThuoc, TextBox txtDia_Chi, TextBox txtMST, TextBox txtD_Thoai, TextBox txtFax)
        {
            if (validateData(txtTen_NhaThuoc,txtDia_Chi,txtMST,txtD_Thoai) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {

                db.NHATHUOC_Insert(ma_nt, txtTen_NhaThuoc.Text, txtDia_Chi.Text, txtMST.Text, txtD_Thoai.Text, txtFax.Text);
                MessageBox.Show("Đã thêm thành công");
            }
        }
        private bool validateData(TextBox txtTen_NhaThuoc, TextBox txtDia_Chi, TextBox txtMST, TextBox txtD_Thoai)
        {
            if (txtTen_NhaThuoc.Text.Trim() == "" || txtDia_Chi.Text.Trim() == "" || txtMST.Text.Trim() == "" || txtD_Thoai.Text.Trim() == "")
            {
                return false;
            }
            return true;
        }

        public void loadData(ListView List)
        {
            List.Items.Clear();
            var list = db.NHATHUOC_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_NHATHUOC);

            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }
        }
        public void loadData(DataPager dp)
        {
            var list = db.NHATHUOC_Get_List();
            var typeorder = list.OrderBy(w => w.TEN_NHATHUOC);
            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            int i = 1;
            foreach (var t in typeorder)
            {
                if (t.TEN_NHATHUOC.Trim() != "")
                {
                    dpSource.Add(new { STT = i, t.DIA_CHI, t.FAX, t.MA_NHATHUOC, t.DIEN_THOAI, t.TEN_NHATHUOC, t.MST });
                    i++;
                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public void updateData(string ma_nt, TextBox txtTen_NhaThuoc, TextBox txtDia_Chi, TextBox txtMST, TextBox txtD_Thoai, TextBox txtFax)
        {
            if (validateData(txtTen_NhaThuoc, txtDia_Chi, txtMST, txtD_Thoai) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {

                db.NHATHUOC_Update(ma_nt,ma_nt, txtTen_NhaThuoc.Text, txtDia_Chi.Text, txtMST.Text, txtD_Thoai.Text, txtFax.Text);
                MessageBox.Show("Đã cập nhật!");
            }
        }

        public string deleteData(string ID_Delete, string error)
        {

            error = "";
            if (db.QUAYTN_GetByMaNhathuoc(ID_Delete).Count() > 0 || db.NHANVIEN_GetByMaNhathuoc(ID_Delete).Count() > 0 || db.THUCHI_GetByMaNhathuoc(ID_Delete).Count() > 0)
                error = "Không thể xóa";
            else
            {
                db.NHATHUOC_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;


        }

        public void loadForm_Edit(string ID_Edit,TextBox txtTen_NhaThuoc, TextBox txtDia_Chi, TextBox txtMST, TextBox txtD_Thoai, TextBox txtFax)
        {

            var sl = (NHATHUOC_GetByMaNhathuocResult)db.NHATHUOC_GetByMaNhathuoc(ID_Edit).Single();


           
            txtTen_NhaThuoc.Text = sl.TEN_NHATHUOC;
            txtDia_Chi.Text = sl.DIA_CHI;
            txtD_Thoai.Text = sl.DIEN_THOAI;
            txtMST.Text = sl.MST;
            txtFax.Text = sl.FAX;
        }
    }
}
